import { App } from "../../src/App";
import { Singleton } from "../../src/core/Class";
import Map from "../../src/core/Map";
import { UserData } from "./BaseModule";


export interface SeatInfo{
    seat:number,
    user:UserData,
    obj:any, // class Robot
}
interface RoomCfg{
    ConfId:number, ConfName:string, BetMin:number,Multiple:number
}

export default class BaseSingle extends Singleton<BaseSingle>{
    protected   _userData:any = null;
    protected   _roomData:any = null;
    protected   _roomCfg:RoomCfg[] = [];
    protected   _robots:any[] = [];
    protected   _idleRobots:any[] = [];
    protected   _rankInfo:any[] = [];
    protected   _rooms:any[] = [];
    protected   _players:Map<number,SeatInfo> = new Map<number,SeatInfo>();
    protected   _isPlaying:boolean = false;
    protected   _robotScorePool:number[] = [];


    public get userData():any {return this._userData;}
    public get allRobots():any[] {return this._robots;}
    public get rankInfo():any[] {this._rankInfo.sort((a,b)=>{return b.Coin - a.Coin;}); return this._rankInfo;}
    public get roomData():any[] {return this._roomData;}

    public get randomRoom():any{
        if (!this._roomData)this._roomData = App.Util.randomArray(this._roomCfg);
        return this._roomData;
    };

    public createOneRobot<T>(cls: { new(): T },seat:number,...param:any): T {
        const data:UserData = App.Util.randomArray(this._idleRobots);
        if (data){
            this._idleRobots.splice(this._idleRobots.indexOf(data),1);
            let obj = new cls();
            // @ts-ignore
            obj.init(data,seat,param);
            this._players.inset(seat,{seat:seat,user:data,obj:obj});
            console.log(`idle robot count:${this._idleRobots.length}`);
            return obj;
        }

        return null;
    } 

    public entryRoom(){
        this.onEntry();
    }
    public exitRoom() {
        this.onExit();
    }
    public startGame(){
        this.onStart();
    }
    public fireCard(list:number[]){ 
        this.onFireCard(list);
    }
    public trusteeship(){
        this.onTrusteeship();
    }

    protected onInit(){
        this._robotScorePool = App.getCfg('scorePool');
        this._rooms = App.getCfg('rooms');
        const Nams = App.getCfg('users');
        const Avatars = App.getCfg('avatars');
        const carry = App.getCfg('robotCarry');
        const _avatar = ():number=>{return Avatars[App.Util.random(2,Avatars.length-1)]};
        const _coin = ():number=>{return App.Util.random(carry[0],carry[1])};
        const _userID = ():number=>{return App.Util.random(100000,199999)};
        this._userData = App.getLocalStorage(App.LOCAL_KEY.USER);
        if (App.Util.isInvalid(this._userData)){
            let idx = App.Util.random(0,Nams.length-1);
            const self = Nams[idx];
            Nams.splice(idx,1);
            this._userData = {
                UserName: self.UserName, 
                OpenID:self.OpenID, 
                Avatar: _avatar(), 
                Coin: _coin(), 
                UserID: _userID(),
                Token:''
            };
            App.setLocalStorage(this._userData,App.LOCAL_KEY.USER);
        }

        Nams.forEach((it)=>{
            const isSelf = it.OpenID === this._userData.OpenID;
            it['Avatar'] = !isSelf ? _avatar() : this._userData.Avatar;
            it['Coin'] =  !isSelf ? _coin() : this._userData.Coin;
            it['UserID'] =  !isSelf ? _userID() : this._userData.UserID;
            it['Token'] =  '';
            it['IsUse'] = false;
            this._rankInfo.push(it);
            !isSelf && this._robots.push(it);
        });
        this._roomData = null;
        this._roomCfg = App.getCfg(`rooms`).slice(0);

        this._idleRobots = this._robots.slice(0);
        this._rankInfo.sort((a,b)=>{return b.Coin - a.Coin;});
    }
    protected onStart(){};
    protected onEntry(){}
    protected onFireCard(list:number[]){}
    protected onExit(){}
    protected onTrusteeship(){}
    // 以上几个方法需要具体的游戏GameModule实现
    //----------------------------------------------------------
}